#!/usr/bin/env python3
# coding:utf-8
import time
import paho.mqtt.client as mqtt
import struct

# MQTT Broker地址和端口
MQTT_BROKER = "localhost"
MQTT_PORT = 1883

# 发布消息的主题
TOPIC = "test/binary_topic"

# 创建MQTT客户端实例
client = mqtt.Client()

# 连接到MQTT Broker
client.connect(MQTT_BROKER, MQTT_PORT, 60)

count = 1111
try:
    while True:
        # 准备二进制数据
        data = struct.pack("!I", count)  # 示例：打包一个整数

        # 发布二进制数据
        client.publish(TOPIC, data)
        print(f"Published binary data to topic '{TOPIC}'")

        time.sleep(1)  # 每隔1秒发布一次数据
        count += 1
except KeyboardInterrupt:
    print("Exiting...")

# 断开连接
client.disconnect()
